Completed
Push — master ( 9da91a...c60c2b )
by Maxence
02:22
created

actions.addMemberResult   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 3
nc 3
nop 1
1
/*
2
 * Circles - Bring cloud-users closer together.
3
 *
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later. See the COPYING file.
6
 *
7
 * @author Maxence Lange <[email protected]>
8
 * @copyright 2017
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
26
/** global: OC */
27
/** global: OCA */
28
/** global: Notyf */
29
30
/** global: nav */
31
/** global: elements */
32
/** global: curr */
33
/** global: api */
34
35
36
var actions = {
37
38
39
	joinCircleResult: function (result) {
40
		if (result.status === 0) {
41
			OCA.notification.onFail(
42
				t('circles', "Cannot join this circle") + ': ' +
43
				((result.error) ? result.error : t('circles', 'no error message')));
44
			return;
45
		}
46
47
		elements.removeMemberslistEntry(result.member.user_id);
48
		if (result.member.level === 1) {
49
			OCA.notification.onSuccess(
50
				t('circles', "You have successfully joined this circle"));
51
		} else {
52
			OCA.notification.onSuccess(
53
				t('circles', "You have requested an invitation to join this circle"));
54
		}
55
		actions.selectCircle(result.circle_id);
56
	},
57
58
59
	leaveCircleResult: function (result) {
60
		if (result.status === 1) {
61
62
			elements.mainUIMembers.children("[member-id='" + result.name + "']").each(
63
				function () {
64
					$(this).hide(300);
65
				});
66
67
			actions.selectCircle(result.circle_id);
68
			OCA.notification.onSuccess(
69
				t('circles', "You have successfully left this circle"));
70
			return;
71
		}
72
73
		OCA.notification.onFail(
74
			t('circles', "Cannot leave this circle") + ': ' +
75
			((result.error) ? result.error : t('circles', 'no error message')));
76
	},
77
78
79
	destroyCircleResult: function (result) {
80
		if (result.status === 1) {
81
82
			actions.unselectCircle(result.circle_id);
83
			OCA.notification.onSuccess(
84
				t('circles', "You have successfully destroyed this circle"));
85
			return;
86
		}
87
88
		OCA.notification.onFail(
89
			t('circles', "Cannot destroy this circle") + ': ' +
90
			((result.error) ? result.error : t('circles', 'no error message')));
91
	},
92
93
94
	createCircleResult: function (result) {
95
		var type = actions.getStringTypeFromType(result.type);
96
97
		if (result.status === 1) {
98
			OCA.notification.onSuccess(t('circles', " {type} '{name}' created", {
99
				type: type,
100
				name: result.name
101
			}));
102
			nav.displayCirclesList(result.circle.type);
103
			actions.selectCircle(result.circle.id);
104
			return;
105
		}
106
107
		OCA.notification.onFail(
108
			t('circles', " {type} '{name}' NOT created", {
109
				type: type,
110
				name: result.name
111
			}) + ': ' +
112
			((result.error) ? result.error : t('circles', 'no error message')));
113
	},
114
115
116
	selectCircleResult: function (result) {
117
118
		elements.mainUIMembers.emptyTable();
119
		if (result.status < 1) {
120
			OCA.notification.onFail(
121
				t('circles', 'Issue while retrieving the details of a circle') + '" ' +
122
				((result.error) ? result.error : t('circles', 'no error message')));
123
			return;
124
		}
125
126
		elements.navigation.children('.circle').removeClass('selected');
127
		elements.navigation.children(".circle[circle-id='" + result.circle_id + "']").each(
128
			function () {
129
				$(this).addClass('selected');
130
			});
131
132
		elements.emptyContent.hide(800);
133
		elements.mainUI.fadeIn(800);
134
		curr.circle = result.circle_id;
135
		curr.circleLevel = result.details.user.level;
136
137
		nav.displayCircleDetails(result.details);
138
		nav.displayMembersInteraction(result.details);
139
		nav.displayMembers(result.details.members);
140
	},
141
142
143
	listCirclesResult: function (result) {
144
145
		if (result.status < 1) {
146
			OCA.notification.onFail(
147
				t('circles', 'Issue while retrieving the list of circles') + '; ' +
148
				((result.error) ? result.error : t('circles', 'no error message')));
149
			return;
150
		}
151
152
		elements.resetCirclesList();
153
154
		var data = result.data;
155
		for (var i = 0; i < data.length; i++) {
156
			var tmpl = elements.generateTmplCircle(data[i]);
157
			elements.navigation.append(
158
				'<div class="circle" circle-id="' + data[i].id + '">' + tmpl + '</div>');
159
		}
160
161
		elements.navigation.children('.circle').on('click', function () {
162
			actions.selectCircle($(this).attr('circle-id'));
163
		});
164
	},
165
166
167
	selectCircle: function (circle_id) {
168
		curr.searchUser = '';
169
		elements.addMember.val('');
170
171
		api.detailsCircle(circle_id, actions.selectCircleResult);
172
	},
173
174
175
	unselectCircle: function (circle_id) {
176
		elements.mainUIMembers.emptyTable();
177
		elements.navigation.children(".circle[circle-id='" + circle_id + "']").remove();
178
		elements.emptyContent.show(800);
179
		elements.mainUI.fadeOut(800);
180
181
		curr.circle = 0;
182
		curr.circleLevel = 0;
183
	},
184
185
186
	/**
187
	 *
188
	 * @param search
189
	 */
190
	searchMembersRequest: function (search) {
191
192
		if (curr.searchUser === search) {
193
			return;
194
		}
195
196
		curr.searchUser = search;
197
198
		$.get(OC.linkToOCS('apps/files_sharing/api/v1', 1) + 'sharees',
199
			{
200
				format: 'json',
201
				search: search,
202
				perPage: 200,
203
				itemType: 'principals'
204
			}, actions.searchMembersResult);
205
	},
206
207
208
	searchMembersResult: function (response) {
209
210
		elements.membersSearchResult.children().remove();
211
212
		if (response === null ||
213
			(response.ocs.data.users.length === 0 && response.ocs.data.exact.users.length === 0)) {
214
			elements.membersSearchResult.fadeOut(0);
215
			return;
216
		}
217
218
		elements.fillMembersSearch(response.ocs.data.exact.users, response.ocs.data.users);
219
220
		$('.members_search').on('click', function () {
221
			api.addMember(curr.circle, $(this).attr('searchresult'),
222
				actions.addMemberResult);
223
		});
224
		elements.membersSearchResult.fadeIn(300);
225
	},
226
227
228
	addMemberResult: function (result) {
229
230
		if (result.status === 1) {
231
			OCA.notification.onSuccess(
232
				t('circles', "Member '{name}' successfully added to the circle",
233
					{name: result.name}));
234
235
			nav.displayMembers(result.members);
236
			return;
237
		}
238
		OCA.notification.onFail(
239
			t('circles', "Member '{name}' NOT added to the circle", {name: result.name}) + ': ' +
240
			((result.error) ? result.error : t('circles', 'no error message')));
241
	},
242
243
244
	getStringTypeFromType: function (type) {
245
		switch (type) {
0 ignored issues
show
Coding Style introduced by
As per coding-style, switch statements should have a default case.
Loading history...
246
			case '1':
247
				return t('circles', 'Personal circle');
248
			case '2':
249
				return t('circles', 'Hidden circle');
250
			case '4':
251
				return t('circles', 'Private circle');
252
			case '8':
253
				return t('circles', 'Public circle');
254
		}
255
256
		return t('circles', 'Circle');
257
	},
258
259
260
	removeMemberResult: function (result) {
261
		if (result.status === 1) {
262
263
			elements.rightPanel.fadeOut(300);
264
			elements.mainUIMembers.children("[member-id='" + result.name + "']").each(
265
				function () {
266
					$(this).hide(300);
267
				});
268
			OCA.notification.onSuccess(
269
				t('circles', "Member '{name}' successfully removed from the circle",
270
					{name: result.name}));
271
			return;
272
		}
273
274
		OCA.notification.onFail(
275
			t('circles', "Member '{name}' NOT removed from the circle", {name: result.name}) +
276
			': ' +
277
			((result.error) ? result.error : t('circles', 'no error message')));
278
	},
279
280
281
	/**
282
	 *
283
	 */
284
	onEventNewCircle: function () {
285
		curr.circle = 0;
286
		curr.circleLevel = 0;
287
288
		elements.circlesList.children('div').removeClass('selected');
289
		elements.emptyContent.show(800);
290
		elements.mainUI.fadeOut(800);
291
	},
292
293
294
	/**
295
	 *
296
	 */
297
	onEventNewCircleName: function () {
298
		this.onEventNewCircle();
299
		nav.displayOptionsNewCircle((elements.newName.val() !== ''));
300
	},
301
302
303
	/**
304
	 *
305
	 */
306
	onEventNewCircleType: function () {
307
		this.onEventNewCircle();
308
		elements.newTypeDefinition.children('div').fadeOut(300);
309
		$('#circles_new_type_' + elements.newType.children('option:selected').val()).fadeIn(
310
			300);
311
	}
312
313
314
};
315